﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SARUserControls
{
    /// <summary>
    /// 场景设计控件
    /// </summary>
    public partial class ObjectScene : UserControl
    {
        public ObjectScene()
        {
            this.InitializeComponent();
            totleItem = this.stackPanelContainer.Children.Count;
            foreach (UIElement menuitem in stackPanelContainer.Children)
            {
                ((ObjectMenuItem)menuitem).Tag = this;
            }

        }

        /// <summary>
        /// 添加一个菜单项目
        /// </summary>
        /// <param name="newItem"></param>
        public void SARAddItem(ObjectMenuItem newItem)
        {
            stackPanelContainer.Children.Add(newItem);
        }

        private void mouseCaptureUp_MouseEnter(object sender, MouseEventArgs e)
        {
            this.mouseCaptureUp.Opacity = 0.1;
        }

        private void mouseCaptureUp_MouseLeave(object sender, MouseEventArgs e)
        {
            this.mouseCaptureUp.Opacity = 0;
        }

        private void mouseCaptureUp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.mouseCaptureUp.Opacity = 0.3;
            if (currentItem <= 0)
            {
                return;
            }
            this.stackPanelContainer.Margin = new Thickness(stackPanelContainer.Margin.Left, stackPanelContainer.Margin.Top + 34, stackPanelContainer.Margin.Right, stackPanelContainer.Margin.Bottom);
            currentItem--;
        }

        private void mouseCaptureUp_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.mouseCaptureUp.Opacity = 0.1;
        }

        private void mouseCaptureDown_MouseEnter(object sender, MouseEventArgs e)
        {
            this.mouseCaptureDown.Opacity = 0.1;
        }

        private void mouseCaptureDown_MouseLeave(object sender, MouseEventArgs e)
        {
            this.mouseCaptureDown.Opacity = 0;
        }

        private void mouseCaptureDown_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.mouseCaptureDown.Opacity = 0.3;
            if (totleItem <= 6 || currentItem + 6 == totleItem)
            {
                return;
            }
            this.stackPanelContainer.Margin = new Thickness(stackPanelContainer.Margin.Left, stackPanelContainer.Margin.Top - 34, stackPanelContainer.Margin.Right, stackPanelContainer.Margin.Bottom);
            currentItem++;
        }

        private void mouseCaptureDown_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.mouseCaptureDown.Opacity = 0.1;
        }

        private int totleItem = 0;
        private int currentItem = 0;


        private double realwidth = 650;

        /// <summary>
        /// 设置或者获取场景的宽度
        /// </summary>
        public double SARRealwidth
        {
            get { return realwidth; }
            set
            {
                realwidth = value;
                foreach (UIElement ins in gridDisplay.Children)
                {
                    if (ins is ObjectInstance)
                    {
                        ((ObjectInstance)ins).SARRealWidth = value;
                    }
                }
            }
        }
        private double realheight = 230;
        
        /// <summary>
        /// 场景的真实高度
        /// </summary>
        public double SARRealheight
        {
            get { return realheight; }
            set
            {
                realheight = value;
                foreach (UIElement ins in gridDisplay.Children)
                {
                    if (ins is ObjectInstance)
                    {
                        ((ObjectInstance)ins).SARRealHeight = value;
                    }
                }
            }
        }

        private void gridDragArea_DragEnter(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent("myFormat") ||
        sender == e.Source)
            {
                e.Effects = DragDropEffects.None;
            }

        }

        private void gridDragArea_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(List<ObjectInstance>)))
            {
                List<ObjectInstance> instances = e.Data.GetData(typeof(List<ObjectInstance>)) as List<ObjectInstance>;
                Point mousepoint = e.GetPosition(this.gridDragArea);
                double mousex = realwidth / 650 * mousepoint.X;
                double mousey = realheight - (realheight / 230 * mousepoint.Y);

                foreach (ObjectInstance ins in instances)
                {
                    ins.SARPostion = new Point(ins.SARPostion.X + mousex, ins.SARPostion.Y + mousey);

                    if (ins.SARPostion.X < 0 || ins.SARPostion.X > realwidth || ins.SARPostion.Y < 0 || ins.SARPostion.Y > realheight)
                    {
                        continue;
                    }

                    this.gridDisplay.Children.Add(ins);
                }
            }

            if (e.Data.GetDataPresent(typeof(ObjectInstance)))
            {
                ObjectInstance instance = e.Data.GetData(typeof(ObjectInstance)) as ObjectInstance;
                
                Point mousepoint = e.GetPosition(this.gridDragArea);
                double mousex = realwidth / 650 * mousepoint.X;
                double mousey = realheight - (realheight / 230 * mousepoint.Y);

                instance.SARPostion = new Point(mousex, mousey);
            }
        }

        /// <summary>
        /// 场景中的所有实例对象
        /// </summary>
        public List<ObjectInstance> SARAllInsctances
        {
            get
            {
                List<ObjectInstance> result = new List<ObjectInstance>();

                foreach (UIElement ins in gridDisplay.Children)
                {
                    if (ins is ObjectInstance)
                    {
                        result.Add((ObjectInstance)ins);
                    }
                }
                return result;
            }
        }

    }
}